package com.swinw.asumall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.swinw.asumall.product.service.CategoryService;
import com.swinw.asumall.product.vo.AttrGroupRelationVo;
import com.swinw.asumall.product.vo.AttrRespVo;
import com.swinw.asumall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.swinw.asumall.product.entity.AttrEntity;
import com.swinw.asumall.product.service.AttrService;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.R;



/**
 * 商品属性
 *
 * @author 罗义华
 * @email 2407550328@qq.com
 * @date 2020-10-04 12:33:33
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/base/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params ,@PathVariable("catId")Long catId){
        PageUtils page = attrService.queryPage(params,catId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        Long[] catalogPath = categoryService.findCatalogPath(attrRespVo.getCatelogId());
        attrRespVo.setCatelogPath(catalogPath);
        System.out.println(attrRespVo);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除 attr/relation/delete
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
//		attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeAttrAndGropu(Arrays.asList(attrIds));
        return R.ok();
    }

    //sale/list/0
    /**
     * 查询销售属性
     */
    @GetMapping("sale/list/{catId}")
    public R getSaleAttr(@RequestParam Map<String, Object> params ,@PathVariable("catId")Long catId){
        PageUtils page = attrService.querySaleAttr(params,catId);
        return R.ok().put("page",page);
    }
}
